<?php

class ProductsController extends FController
{
	
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow', // allow authenticated users to access all actions
				'users'=>array('@'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		); 
	}
	
	public function actionCreate()
	{
		$model=new Products;
		
		$Caregory = Category::model()->findAll();
		
		$CareAry=array();
		if(is_array($CareAry)){
			foreach ($Caregory as $Cval)
			{
				$CareAry[ $Cval['ID'] ]=$Cval->ENName.'/'.$Cval->ZHName;
			}
		}

		$upload_dir=Yii::app()->params['uploadFile'];
		$base_upload_dir=Yii::app()->basePath.DS.$upload_dir;

			
		if(isset($_POST['Products']))
		{
			$model->attributes=$_POST['Products'];
			for ($a=1;$a<=5;$a++){
				$pic_name='Pic'.$a;
				$file=CUploadedFile::getInstance($model,$pic_name);	
				if($file){
					$newname=date('YmdHmss',time()).$a.'.'.$file->extensionName;
					$picpath=$upload_dir.'product'.DS.$newname;
					$picpath=str_replace(DS, '/',$picpath);
					$model->$pic_name = $picpath;
					$file->saveAs($model->$pic_name);//上传到服务器
				}		
				unset($file);
			}		
			if($model->save())
				$this->redirect(array('index'));
		}
				
		$this->render('create',array(
			'model'=>$model,
			'CareAry'=>$CareAry,
			'flagDatas' => array( '1' => '显示' , '0' => '不显示')
		));		
	}

	public function actionDel($ID)
	{
		$ID=intval($ID);
		if(!empty($ID))
		{
			// we only allow deletion via POST request
			$model=new Products;
			$Pic1=Products::model()->findByPk($ID)->Pic1;
			$Pic2=Products::model()->findByPk($ID)->Pic2;
			$Pic3=Products::model()->findByPk($ID)->Pic3;
			$Pic4=Products::model()->findByPk($ID)->Pic4;
			$Pic5=Products::model()->findByPk($ID)->Pic5;
			if($Pic1 && file_exists($Pic1))
			{
				unlink($Pic1);
			}
			if($Pic2 && file_exists($Pic2))
			{
				unlink($Pic2);
			}
			if($Pic3 && file_exists($Pic3))
			{
				unlink($Pic3);
			}
			if($Pic4 && file_exists($Pic4))
			{
				unlink($Pic4);
			}
			if($Pic5 && file_exists($Pic5))
			{
				unlink($Pic5);
			}													
			if(Products::model()->deleteByPk($ID))
			{
				$this->redirect(array('index'));
			}
		}
		else{
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}		
	}
	
	public function actionDelimg($ID,$return)
	{
		$return=intval($return);
		
		$model=new Products;
		$model=Products::model()->findByPk($return);

		$col="Pic".intval($ID);
		$value=$model->$col;
		if(isset($value) &&  file_exists($value))
		{
			unlink($value);
		}
		$model->attributes =array($col=>"");
		$model->$col="";

		if($model->save()){
				$this->redirect($this->createUrl("products/update",array('ID'=>$return)));
		}else{
			//throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}		
		
		
		
	}

	public function actionIndex()
	{
		//取得每页显示多少个
		$pageNum = Yii::app()->params['adminGirdPageNum'];
		$totalNum = Products::model()->count();
		$pages=new CPagination($totalNum);
		$pages->pageSize=$pageNum;
		$criteria=new CDbCriteria();
		$criteria->order ="Sorts ASC";
		$pages->applyLimit($criteria);
		$datas = Products::model()->findAll( 'ID>:ID ORDER BY Sorts ASC limit :offset,:limit' , array(
			':ID' => 0 ,
			':offset' => $pages->currentPage*$pages->pageSize ,
			':limit' => $pages->pageSize,
		) );

		foreach ($datas as $key=>&$value){
			if(isset($value['Pic1']) && !empty($value['Pic1']))
			{
				$pic=$value['Pic1'];	
			}elseif(isset($value['Pic2']) && !empty($value['Pic2'])){
				$pic=$value['Pic2'];
			}elseif(isset($value['Pic3']) && !empty($value['Pic3'])){
				$pic=$value['Pic3'];
			}elseif(isset($value['Pic4']) && !empty($value['Pic4'])){
				$pic=$value['Pic4'];
			}elseif(isset($value['Pic5']) && !empty($value['Pic5'])){
				$pic=$value['Pic5'];
			}else {
				$pic="images/no_image.jpg";
			}

			if($value['CategoryId'])
			{
				$Category=Category::model()->findByPk($value['CategoryId']);
			}else{
				$Category['ENName']="";
			}
			$value = array(
				'ID' => $value['ID'],
				'CategoryId'=>$Category['ENName'],
				'ENName' => $value['ENName'],
				'ZHName' => $value['ZHName'],
				'PicPath' => '<img src="' . $pic .'" size="60" width="90" />',
				'Sorts'   =>$value['Sorts'],
				'IndexFlag' => $value['IndexFlag'] == 1 ? '是' : '否' ,
			);			
		}
		$this->render('index',array(
			'totalNum' => $totalNum ,
			'datas' => $datas,
			'pages' => $pages
		));		
	}

	public function actionUpdate($ID)
	{
		$ID=intval($ID);
		$model=new Products;
		
		
		$Caregory = Category::model()->findAll();
		
		$CareAry=array();
		if(is_array($CareAry)){
			foreach ($Caregory as $Cval)
			{
				$CareAry[ $Cval['ID'] ]=$Cval->ENName.'/'.$Cval->ZHName;
			}
		}
		
			
		if(!empty($ID))
		{
			$model=Products::model()->findByPk($ID);
			if(!$model->ID)
			{
				throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
			}
		}else{
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}
				
		$upload_dir=Yii::app()->params['uploadFile'];
		$base_upload_dir=Yii::app()->basePath.DS.$upload_dir;

			
		if(isset($_POST['Products']))
		{
			$model->attributes=$_POST['Products'];
			for ($a=1;$a<=5;$a++){
				$pic_name='Pic'.$a;
				$file=CUploadedFile::getInstance($model,$pic_name);	
				if($file){
					$newname=date('YmdHmss',time()).$a.'.'.$file->extensionName;
					$picpath=$upload_dir.'product'.DS.$newname;
					$picpath=str_replace(DS, '/',$picpath);
					$model->$pic_name = $picpath;
					$file->saveAs($model->$pic_name);//上传到服务器
				}		
				unset($file);
			}		
			if($model->save())
				$this->redirect(array('index'));
		}
				
		$this->render('create',array(
			'model'=>$model,
			'CareAry'=>$CareAry,		
			'flagDatas' => array( '1' => '显示' , '0' => '不显示')
		));				
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}